package org.example.innovate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.innovate.common.CommonResponse;
import org.example.innovate.domain.Comment;
import org.example.innovate.domain.User;
import org.example.innovate.service.CommentService;
import org.example.innovate.mapper.CommentMapper;
import org.example.innovate.util.JwtUtil;
import org.example.innovate.vo.CommentVo;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
* @author 马鑫
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2025-01-17 15:45:39
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
    @Resource
    private CommentMapper commentMapper;

    @Override
    public CommonResponse<CommentVo> addComment(Comment comment, HttpServletRequest request)
    {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        comment.setUserId(user.getUserId());
        commentMapper.insert(comment);
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        queryWrapper.orderByDesc("comment_id");
        queryWrapper.last("limit 1");
        Comment comment1=commentMapper.selectOne(queryWrapper);
        CommentVo commentVo=new CommentVo();
        commentVo.setName(user.getUsername());
        commentVo.setContent(comment.getContent());
        Date now=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.HOUR_OF_DAY, 8);
        Date eightHoursAgo = cal.getTime();
        commentVo.setTime(eightHoursAgo);
        commentVo.setLikes(0);
        commentVo.setId(user.getUserId());
        commentVo.setComment_id(comment1.getCommentId());
        return CommonResponse.createForSuccess("评论成功",commentVo);
    }

    @Override
    public CommonResponse<Comment> deleteComment(Comment comment)
    {
        if(comment.getReply()==1)
        {
            QueryWrapper<Comment> wrapper=new QueryWrapper<>();
            wrapper.eq("comment_id",comment.getCommentId());
            wrapper.eq("reply",1);
            commentMapper.delete(wrapper);
        }
        else {
            QueryWrapper<Comment> wrapper=new QueryWrapper<>();
            wrapper.eq("comment_id",comment.getCommentId());
            wrapper.eq("reply",0);
            commentMapper.delete(wrapper);
            wrapper=new QueryWrapper<>();
            wrapper.eq("foreign_id",comment.getCommentId());
            wrapper.eq("reply",1);
            commentMapper.delete(wrapper);
        }
        return CommonResponse.createForSuccess("删除成功",comment);
    }
}




